<?php

/**
 * This is the model class for table "{{activity}}".
 *
 * The followings are the available columns in table '{{activity}}':
 * @property string $id
 * @property string $token
 * @property integer $city_id
 * @property integer $user_id
 * @property string $title
 * @property integer $is_show
 * @property integer $is_recommend
 * @property string $people
 * @property string $theme
 * @property integer $fee
 * @property string $phone
 * @property string $thumb
 * @property integer $go_time
 * @property integer $back_time
 * @property string $activity_time
 * @property double $price
 * @property integer $share_num
 * @property integer $click_num
 * @property string $facilitator
 * @property integer $is_pay
 * @property string $payment_info
 * @property string $fee_info
 * @property string $travel_tips
 * @property string $content
 * @property integer $create_time
 * @property integer $update_time
 * @property integer $listorder
 * @property integer $frond_recommend
 */
class Activity extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{activity}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('city_id', 'required'),
			array('city_id, user_id, is_show, is_recommend, fee, go_time, back_time, share_num, click_num, is_pay, create_time, update_time, listorder, frond_recommend', 'numerical', 'integerOnly'=>true),
			array('price', 'numerical'),
			array('token', 'length', 'max'=>20),
			array('title, facilitator', 'length', 'max'=>100),
			array('people, theme, phone, activity_time', 'length', 'max'=>50),
			array('thumb', 'length', 'max'=>120),
			array('payment_info, fee_info, travel_tips, content', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, token, city_id, user_id, title, is_show, is_recommend, people, theme, fee, phone, thumb, go_time, back_time, activity_time, price, share_num, click_num, facilitator, is_pay, payment_info, fee_info, travel_tips, content, create_time, update_time, listorder, frond_recommend', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'token' => 'Token',
			'city_id' => 'City',
			'user_id' => 'User',
			'title' => 'Title',
			'is_show' => 'Is Show',
			'is_recommend' => 'Is Recommend',
			'people' => 'People',
			'theme' => 'Theme',
			'fee' => 'Fee',
			'phone' => 'Phone',
			'thumb' => 'Thumb',
			'go_time' => 'Go Time',
			'back_time' => 'Back Time',
			'activity_time' => 'Activity Time',
			'price' => 'Price',
			'share_num' => 'Share Num',
			'click_num' => 'Click Num',
			'facilitator' => 'Facilitator',
			'is_pay' => 'Is Pay',
			'payment_info' => 'Payment Info',
			'fee_info' => 'Fee Info',
			'travel_tips' => 'Travel Tips',
			'content' => 'Content',
			'create_time' => 'Create Time',
			'update_time' => 'Update Time',
			'listorder' => 'Listorder',
			'frond_recommend' => 'Frond Recommend',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('token',$this->token,true);
		$criteria->compare('city_id',$this->city_id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('is_show',$this->is_show);
		$criteria->compare('is_recommend',$this->is_recommend);
		$criteria->compare('people',$this->people,true);
		$criteria->compare('theme',$this->theme,true);
		$criteria->compare('fee',$this->fee);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('thumb',$this->thumb,true);
		$criteria->compare('go_time',$this->go_time);
		$criteria->compare('back_time',$this->back_time);
		$criteria->compare('activity_time',$this->activity_time,true);
		$criteria->compare('price',$this->price);
		$criteria->compare('share_num',$this->share_num);
		$criteria->compare('click_num',$this->click_num);
		$criteria->compare('facilitator',$this->facilitator,true);
		$criteria->compare('is_pay',$this->is_pay);
		$criteria->compare('payment_info',$this->payment_info,true);
		$criteria->compare('fee_info',$this->fee_info,true);
		$criteria->compare('travel_tips',$this->travel_tips,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('create_time',$this->create_time);
		$criteria->compare('update_time',$this->update_time);
		$criteria->compare('listorder',$this->listorder);
		$criteria->compare('frond_recommend',$this->frond_recommend);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Activity the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
    public function getList($city_id){
        $criteria = new CDbCriteria;
        $criteria->addCondition("city_id=$city_id");
        $criteria->order = "id DESC";
        $dataProvider= new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'pagination'=>array(
                'pageSize'=>20,
            ),
        ));
        return $dataProvider;
    }
    public function getSearchList($city_id,$keyword){
        $criteria = new CDbCriteria;
        $criteria->addCondition("city_id=$city_id");
        $criteria->addSearchCondition('title',$keyword,true,'AND');
        $criteria->order = "id DESC";
        $dataProvider= new CActiveDataProvider($this, array(
            'criteria' => $criteria,
            'pagination'=>array(
                'pageSize'=>20,
            ),
        ));
        return $dataProvider;
    }
    public function getFrontCountList($city_id,$theme,$people,$fee,$star,$activity){
        $time = time();
        $db = Yii::app()->db->createCommand();
        $db->select('count(*) as count');
        $db->from('{{activity}}');
        $db->andWhere('city_id=:city_id and is_show=:is_show and go_time>:go_time',array(':city_id'=>$city_id,':is_show'=>1,':go_time'=>$time));
        if($theme){
            $db->andWhere('FIND_IN_SET(:theme,theme)',array(':theme'=>$theme));
        }
        if($people){
            $db->andWhere('FIND_IN_SET(:people,people)',array(':people'=>$people));
        }
        if($fee){
            $db->andWhere('fee=:fee',array(':fee'=>$fee));
        }
        if($star){
            //获取本周 起始时间戳 与截止时间戳
            $startTime = strtotime('last Monday');
            $endTime = strtotime('next Monday');
            //下个礼拜的时间戳截止  604800
            $secendEndTime = $endTime+604800;
            if($star==2){
                $db->andWhere('go_time >:go_time and back_time <:back_time ',array(':go_time'=>$endTime,':back_time'=>$secendEndTime));
            }elseif($star==3){
                $db->andWhere('go_time >:go_time',array(':go_time'=>$secendEndTime));
            }else{
                $db->andWhere('go_time >:go_time and back_time <:back_time ',array(':go_time'=>$startTime,':back_time'=>$endTime));
            }
        }
        if($activity){
            $db->andWhere('activity_time=:activity_time',array(':activity_time'=>$activity));
        }
        $info = $db->queryRow();
        return $info['count'];
    }
    public function getFutureEvent($city_id){
        $time = time();
        $db = Yii::app()->db->createCommand();
        $db->select('title,go_time,token,thumb,price');
        $db->from('{{activity}}');
        $db->andWhere('city_id=:city_id and is_show=:is_show and is_recommend=:is_recommend and go_time>:go_time',array(':city_id'=>$city_id,':is_show'=>1,':is_recommend'=>1,':go_time'=>$time));
        $db->order('listorder desc,id desc');
        $info = $db->queryAll();
        return $info;
    }


    public function getFrontList($city_id,$first,$total,$theme,$people,$fee,$star,$activity){
        $time = time();
        $db = Yii::app()->db->createCommand();
        $db->select('title,go_time,token,thumb,price');
        $db->from('{{activity}}');
        $db->andWhere('city_id=:city_id and is_show=:is_show and go_time>:go_time',array(':city_id'=>$city_id,':is_show'=>1,':go_time'=>$time));
        if($theme){
            $db->andWhere('FIND_IN_SET(:theme,theme)',array(':theme'=>$theme));
        }
        if($people){
            $db->andWhere('FIND_IN_SET(:people,people)',array(':people'=>$people));
        }
        if($fee){
            $db->andWhere('fee=:fee',array(':fee'=>$fee));
        }
        if($star){
            //获取本周 起始时间戳 与截止时间戳
            $startTime = strtotime('last Monday');
            $endTime = strtotime('next Monday');
            //下个礼拜的时间戳截止  604800
            $secendEndTime = $endTime+604800;
            if($star==2){
                $db->andWhere('go_time >:go_time and back_time <:back_time ',array(':go_time'=>$endTime,':back_time'=>$secendEndTime));
            }elseif($star==3){
                $db->andWhere('go_time >:go_time',array(':go_time'=>$secendEndTime));
            }else{
                $db->andWhere('go_time >:go_time and back_time <:back_time ',array(':go_time'=>$startTime,':back_time'=>$endTime));
            }
        }
        if($activity){
            $db->andWhere('activity_time=:activity_time',array(':activity_time'=>$activity));
        }
        $db->order("is_recommend desc,listorder desc,go_time asc");
        $db->limit($total,$first);
        $info = $db->queryAll();
        return $info;
    }
    public function getFrontIndex($city_id,$limit){
        $time = time();
        $db = Yii::app()->db->createCommand();
        $db->select('title,go_time,token,thumb,price');
        $db->from("{{activity}}");
        $db->andWhere('city_id=:city_id and is_show=:is_show and frond_recommend=:frond_recommend and go_time>:go_time',array(':city_id'=>$city_id,':is_show'=>1,':frond_recommend'=>1,':go_time'=>$time));
        $db->order("go_time asc");
        $db->limit($limit);
        $info = $db->queryAll();
        return $info;
    }
}
